import wx
from app import main_iic_process
from app.ReadWriteWindow import ReadWriteWindow, Divider
from app.main_iic_process import *
from app.utils import *

FRAME_WIDTH = 1000
FRAME_HEIGHT = 300
# 字体大小
FONT_SIZE = 14
READ_INTERVAL_MILLS = 1000


class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(FRAME_WIDTH, FRAME_HEIGHT))
        self.initView()
        self.initData()
        self.autoConnected()

    def initView(self):
        self.initMenuBar()
        self.initLayout()

    def initMenuBar(self):
        self.statusBar = wx.StatusBar(self, -1)  # 实例化 wx.StatusBar
        self.statusBar.SetFieldsCount(3)  # 状态栏分成3个区域
        self.statusBar.SetStatusWidths([-1, -1, -1])  # 区域宽度比列，用负数
        self.statusBar.SetStatusText("", 0)  # 给状态栏设文字
        self.SetStatusBar(self.statusBar)  # 将状态栏附加到框架上


    def initLayout(self):
        rootLayout = wx.BoxSizer(wx.VERTICAL)

        contentLayout = wx.BoxSizer(wx.VERTICAL)
        btnLayout = wx.BoxSizer(wx.HORIZONTAL)
        optionLayout = wx.BoxSizer(wx.HORIZONTAL)
        self.stateText = wx.StaticText(self, -1, "未连接", size=(40, 20))
        self.stateText.SetForegroundColour((255, 0, 0))
        optionLayout.Add(self.stateText, 0, flag=wx.LEFT, border=20)

        btnStart = wx.Button(self, -1, "读取状态", pos=(0, 0), size=(100, 40))
        btnStart.Bind(wx.EVT_BUTTON, self.start)
        btnLayout.Add(btnStart, 0)

        attrNameArr = ["状态位", "ir_adc", "ntc_adc", "物温", "环温", "保留参数1", "保留参数2", "保留参数3"]
        self.textviewArr = []
        self.attrViewArr = []
        for i in range(0, 8):
            st = wx.StaticText(self, -1, attrNameArr[i], size=(120, 30), style=wx.ALIGN_CENTER)
            st.SetBackgroundColour((240, 240, 240))
            font = self.GetFont()
            font.SetPointSize(FONT_SIZE)
            st.SetFont(font)
            self.textviewArr.append(st)

            attr_st = wx.TextCtrl(self, -1, value="-", size=(120, 30),style=wx.TE_CENTER)
            attr_st.SetFont(font)
            attr_st.SetBackgroundColour((255, 255, 255))
            self.attrViewArr.append(attr_st)

        box1 = wx.BoxSizer(wx.HORIZONTAL)
        box2 = wx.BoxSizer(wx.HORIZONTAL)
        for i in range(0, 8):
            box1.Add(self.textviewArr[i], proportion=0, flag=wx.EXPAND, border=5)
            box2.Add(self.attrViewArr[i], proportion=0, flag=wx.EXPAND, border=5)

        contentLayout.Add(box1, 0, wx.EXPAND | wx.ALL, 5)
        contentLayout.Add(box2, 0, wx.EXPAND | wx.ALL, 5)
        optionLayout.Add(btnLayout, 0, wx.EXPAND | wx.TOP, border=-10)
        rootLayout.Add(contentLayout, 0, wx.EXPAND)
        rootLayout.Add(optionLayout, 0, wx.EXPAND | wx.TOP, border=20)
        # rootLayout.Add(btnLayout, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.TOP | wx.BOTTOM, border=30)
        self.SetSizer(rootLayout)
        self.SetBackgroundColour((250, 250, 250))
        # rootLayout.Fit(self)

    def initData(self):
        pass

    def autoConnected(self):
        callback = FatriIicCallback(self)
        # 初始化iic和usb连接
        self.master = iic_process(callback=callback)
        # 线程监控iic连接和初始化状态
        iic_dev_thread(self.master)


    def start(self, e):
        pass



    def onCancel(self, e):
        self.master.detect_data_stop()

    def notify(self, stateValArr,ctrlflag, extras=[]):
        pass








class FatriIicCallback(utils.IicCallback):
    def __init__(self, appWindow):
        self.appWindow = appWindow

    def onUSBConnectd(self):
        self.appWindow.stateText.SetForegroundColour(wx.Colour(0, 0, 255))
        self.appWindow.stateText.SetLabel("已连接")

    def onUSBClosed(self):
        self.appWindow.stateText.SetLabel("未连接")
        self.appWindow.stateText.SetForegroundColour(wx.Colour(255, 0, 0))

    def onDevInit(self):
        self.appWindow.stateText.SetForegroundColour(wx.Colour(0, 0, 255))
        self.appWindow.stateText.SetLabel("已就绪")

    def onReceived(self,respData):
        self.appWindow.notify(respData)

    def onStateOutput(self, level="i", msg="", errorCode=-1):
        if level == "e":
            self.appWindow.statusBar.SetStatusText(msg, 1)  # 状态栏错误信息
        else:
            self.appWindow.statusBar.SetStatusText(msg, 0)  # 状态栏信息


app = wx.App()
frame = MainWindow(None, "FATRI-checktool")
frame.Show()
app.MainLoop()
